package com.example.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.Result;
import com.example.common.enums.ResultCodeEnum;
import com.example.entity.Admin;
import com.example.entity.Staff;
import com.example.exception.CustomException;
import com.example.service.AdminService;
import com.example.service.StaffService;
import com.github.pagehelper.PageInfo;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 员工前端操作接口
 **/
@RestController
@RequestMapping("/staff")
public class StaffController{

    @Resource
    private StaffService staffService;

    @Resource
    private AdminService adminService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Staff staff) {
        staffService.add(staff);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        staffService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        staffService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateByCheck(@RequestBody Staff staff) {
        staffService.updateByCheck(staff);
        Admin admin = adminService.selectByWorkCode(staff.getWorkCode());
        if (admin == null) {
            admin = new Admin();
            BeanUtil.copyProperties(staff, admin, CopyOptions.create()
                    .ignoreNullValue()
                    .ignoreError());
            adminService.add(admin);
        } else {
            BeanUtil.copyProperties(staff, admin, CopyOptions.create()
                    .ignoreNullValue()
                    .ignoreError());
            adminService.updateByCheck(admin);
        }
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Staff staff = staffService.selectById(id);
        return Result.success(staff);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Staff staff ) {
        List<Staff> list = staffService.selectAll(staff);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Staff staff,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Staff> page = staffService.selectPage(staff, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * Excel 导出数据
     */
    @PostMapping("/export")      //根据前端传的ids参数，导出对应员工的记录
    public void exportData(HttpServletResponse response,@RequestBody(required = true) List<Integer>  ids) throws IOException {
        if (ObjectUtil.isEmpty(ids)) {
            throw new CustomException(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        staffService.exportDataByIds(response, ids);
    }

}